package com.xinchuang.service.targetSystem.Impl;

import cn.hutool.core.collection.CollectionUtil;
import com.alibaba.fastjson.JSONObject;
import com.xinchuang.common.Result;
import com.xinchuang.common.ResultUtil;
import com.xinchuang.mapper.targetSystem.SystemPositionMapper;
import com.xinchuang.entity.targetSystem.system.SystemPosition;
import com.xinchuang.service.RedisUseService;
import com.xinchuang.service.targetSystem.SysElseService;
import com.xinchuang.service.targetSystem.SysElseService;
import com.xinchuang.util.FileElseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @ClassName SysElseServiceImpl
 * @Description: TODO(写一段描述的话)
 * @Author zhangxuan
 * @Date 2022-05-11 16:40
 * @Verion V1.0
 */
@Service
public class SysElseServiceImpl implements SysElseService {

    @Resource
    private SystemPositionMapper systemPositionMapper;

    @Resource
    private RedisUseService redisUseService;

    @Override
    public Result addPosition(JSONObject jsonObject) {
        String id = jsonObject.getString("id");
        if (StringUtils.isBlank(id)) {
            return ResultUtil.error(500, "数据异常，请联系管理员处理。");
        }
        SystemPosition systemPosition = new SystemPosition();
        systemPosition.setId(id);
        systemPosition.setPositions(CollectionUtil.isNotEmpty(jsonObject.getJSONArray("position")) ? jsonObject.getJSONArray("position").toJSONString() : "[]");
        systemPosition.setViewDistances(CollectionUtil.isNotEmpty(jsonObject.getJSONArray("viewDistances")) ? jsonObject.getJSONArray("viewDistances").toJSONString() : "[]");
        systemPosition.setHeight(null != jsonObject.getString("height") ? jsonObject.getString("height") : "0");
        systemPositionMapper.addSystemPosition(systemPosition);
        redisUseService.updateView(systemPosition);
        return ResultUtil.success();
    }

    @Override
    public Result getPosition(String id) {
        Map<String, Object> resultMap = new HashMap<>();
        SystemPosition position = systemPositionMapper.getPositionById(id);
        if (null != position) {
            resultMap.put("position", FileElseUtil.getStringTransitionList(position.getPositions()));
            resultMap.put("viewDistances", FileElseUtil.getStringTransitionList(position.getViewDistances()));
            resultMap.put("height", FileElseUtil.getStringTransitionDouble(position.getHeight()));
        } else {
            resultMap.put("position", new ArrayList<>());
            resultMap.put("viewDistances", new ArrayList<>());
            resultMap.put("height", 0);
        }
        return ResultUtil.success(resultMap);
    }


}